//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.game.left;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Label;

public class WidgetAnswerPopup implements AnswerPopup {
	private static final String[] STYLE_NAMES = { "answerPopupNormal", "answerPopupAnswered", "answerPopupYes", "answerPopupNo" };
	private static final String[][] LETTERS = { { "1", "2", "3", "4" }, { "A", "B", "C", "D" } };
	private static final int OFFSET_X = 4;
	private static final int OFFSET_Y = 16;
	private final Label label = new Label();
	private final AbsolutePanel parentPanel;
	private int x;
	private int y;

	public WidgetAnswerPopup(AbsolutePanel parentPanel) {
		this.parentPanel = parentPanel;
		label.addStyleDependentName("answerPopup");
		setBackgroundColor(COLOR_NORMAL);
	}

	public void setBackgroundColor(int color) {
		for (String styleName : STYLE_NAMES) {
			label.removeStyleDependentName(styleName);
		}
		label.addStyleDependentName(STYLE_NAMES[color]);
	}

	public void setPosition(int x, int y) {
		this.x = x + OFFSET_X;
		this.y = y + OFFSET_Y;
		if (label.isAttached()) {
			parentPanel.setWidgetPosition(label, this.x, this.y);
		}
	}

	public void show(String s) {
		label.setText(s);
		parentPanel.add(label, x, y);
	}

	protected boolean isSystemMessage(String s) {
		return s.equals(LABEL_ANSWERED) || s.equals(LABEL_TIME_UP) || s.equals(LABEL_NO_ANSWER);
	}

	public void hide() {
		parentPanel.remove(label);
	}

	protected String getLetter(int letterType, int letterIndex) {
		return LETTERS[letterType][letterIndex];
	}
}
